package controllers;

import models.Establishment;
import models.MenuItem;
import models.MenuSection;
import models.Picture;
import play.data.Upload;
import play.mvc.Controller;
import util.FileUtil;

public class Item extends Controller {
	public static void list(long sectionId) {
		MenuSection section = MenuSection.findById(sectionId);
		render(section);
	}

	public static void blank(long sectionId) {
		render(sectionId);
	}

	public static void show(long id, long sectionId) {
		MenuItem item = MenuItem.findById(id);
		render(item, sectionId);
	}

	public static void delete(long id, long sectionId) {
		MenuItem item = MenuItem.findById(id);
		item.delete();
		list(sectionId);
	}
	
	public static void create(MenuItem item, Upload picture, MenuSection section) {
		if (picture != null) {
			Picture newPicture = new Picture();
			newPicture.setMimeType(FileUtil.getExtensaoArquivo(picture.asFile()));
			newPicture.setData(picture.asBytes());
			newPicture.save();
			item.getPictures().add(newPicture);
		}
		section.getItems().add(item);
		section.save();
		list(section.getId());
	}
	
	public static void save(MenuItem item, Upload picture, MenuSection section) {
		if (picture != null) {
			Picture newPicture = new Picture();
			newPicture.setMimeType(FileUtil.getExtensaoArquivo(picture.asFile()));
			newPicture.setData(picture.asBytes());
			newPicture.save();
			item.getPictures().add(newPicture);
		}
		item.save();
		list(section.getId());
	}
}
